/**
 * 所有web-view需要组件都放在这里
 */

/**
 * url    				请求地址不带参数，例如https://www.bing.com
 * query_param_object              请求参数，以对象形式保存，例如{id:3}
 * app   				全局变量，如果用户登陆的化就将openid放在初始化请求里面
 * get_url			传入到web-view组件里面
 */
function bulid_query (url ='',query_param_object ={},app={},options={}){
	var query_param = '?';
	for(var i in query_param_object) {
		if(query_param_object[i] !=='' && query_param_object[i] !==undefined ){
			query_param +=(i+'='+query_param_object[i]+'&');
		}
	}
    for(var i_options in options) {
        if(options[i_options] !=='' && options[i_options] !==undefined ){
            query_param +=(i_options+'='+options[i_options]+'&');
        }
    }
	if(app.globalData.userRole){
        query_param +=('user_id='+app.globalData.userRole.id);
	}
	return url+query_param;
}

/**
 * 获取后台的webView组件
 * app    				app全局变量
 * callback       回调函数
 */
function get_webview_config(app,callback){
    wx.showLoading({'title':'加载网页中','mask':true});
    app.util.request({
        url: 'https://weizhen.1000ask.com'+'/home/web_view_config',
        data: {openid:app.globalData.userRole?app.globalData.userRole.wxOpenId:''},
        header: {'content-type':'application/json'},
        success: function(res) {
            wx.hideLoading();
        	if(res.data.code == 200 && res.data.code !== undefined){
        		callback(res.data.body);
          }else{
            wx.showModal({
                title: '错误',
                content: res.data.message?res.data.message:'请求后台地址错误，请检查网络',
                showCancel:false
            })
          }
        },fall:function(res) {
            wx.hideLoading();
            wx.showModal({
                title: '错误',
                content: '微信错误，请检查网络',
                showCancel:false
            })
        },complete:function(res) {
            wx.hideLoading();
        }
    })
}

/**
 * 获取会员中心的切换组件状态
 * app    				app全局变量
 * callback       回调函数
 */
function get_center_config(app,callback){
    app.util.request({
      url: 'https://weizhen.1000ask.com' +'/home/wz_11_get_center_config',
        data: {openid:app.globalData.userRole?app.globalData.userRole.wxOpenId:''},
        header: {'content-type':'application/json'},
        success: function(res) {
            if(res.data.code == 200 && res.data.code !== undefined){
                callback(res.data.body);
            }
        }
    })
}

/**
 * 获取会员中心页面初始化参数，暂时没有用到
 * app    				app全局变量
 * callback       回调函数
 */
function show_center_page(app,callback){
    app.util.request({
        url: 'https://weizhen.1000ask.com'+'/home/show_center',
        data: {openid:app.globalData.userRole?app.globalData.userRole.wxOpenId:''},
        header: {'content-type':'application/json'},
        success: function(res) {
            if(res.data.code == 200 && res.data.code !== undefined){
                callback(res.data.body);
            }
        }
    })
}

module.exports = {
    show_center_page:show_center_page,
    bulid_query:bulid_query,
    get_webview_config:get_webview_config,
    get_center_config:get_center_config,
}